package anthology.leet_code_hot;
//https://leetcode.cn/problems/remove-element/description/
public class _027移除元素 {
    //双指针
    static public int removeElement(int[] nums, int val) {
        if(nums.length == 0 || nums == null) return 0;
        int left = 0;
        for (int right = 0; right < nums.length; right++) {
            if (nums[right] != val) {
                nums[left] = nums[right];
                left++;
            }
        }
        return left;
    }
    //双指针优化
    static public int removeElement2(int[] nums, int val) {
        if(nums.length == 0 || nums == null) return 0;
        int left = 0;
        int right = nums.length;
        while (left < right){
            if(nums[left] == val)
                nums[left]  = nums[--right];
            else
                left++;
        }
        return left;
    }
}
